package ldh.im.web.controller;

import java.util.Date;
import java.util.Objects;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import ldh.common.PageResult;
import ldh.common.json.JsonViewFactory;
import ldh.common.mvc.DateEditor;
import ldh.im.business.constant.CategoryStatus;
import ldh.im.business.constant.LabelStatus;
import ldh.im.business.pojo.CmsLabel;
import ldh.im.business.pojo.where.CmsCategoryWhere;
import ldh.im.business.pojo.where.CmsLabelWhere;
import ldh.im.business.service.CmsCategoryService;
import ldh.im.business.service.CmsLabelService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* @author: ldh
* @date: 2021-07-04 17:02:35
*/
@Controller
@RequestMapping("cmsLabel")
public class CmsLabelController extends BaseController {

	@Resource(name = "cmsLabelService")
	private CmsLabelService cmsLabelService;
	@Resource(name = "cmsCategoryService")
	private CmsCategoryService cmsCategoryService;

	/**
	 * 字符串转化成时间
	 */
	@InitBinder  
	protected void initBinder(HttpServletRequest request,  
	                              ServletRequestDataBinder binder) throws Exception {  
	    //对于需要转换为Date类型的属性，使用DateEditor进行处理  
	    binder.registerCustomEditor(Date.class, new DateEditor());  
	} 

	@RequiresPermissions("cmsLabel:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save")
    public String save(@ModelAttribute CmsLabel cmsLabel) throws Exception {
    	Objects.requireNonNull(cmsLabel);
		cmsLabel.setUpdateTime(new Date());
    	if (cmsLabel.getId() == null) {
    		cmsLabel.setScore(0.0d);
    		cmsLabel.setViewCount(0L);
    		cmsLabel.setCreateTime(new Date());
    		cmsLabel.setCommentCount(0L);
			cmsLabelService.insert(cmsLabel);
    	} else {
			cmsLabelService.updateById(cmsLabel);
    	}
    	return "redirect:/cmsLabel/list";
    }

	@RequiresPermissions("cmsLabel:save")
    @RequestMapping(method = RequestMethod.POST, value = "/save/json")
    @ResponseBody
    public String saveJson(@ModelAttribute CmsLabelWhere cmsLabel) throws Exception {
    	Objects.requireNonNull(cmsLabel);
    	if (cmsLabel.getId() == null) {
			cmsLabelService.insert(cmsLabel);
		} else {
			cmsLabelService.updateById(cmsLabel);
		}
        return JsonViewFactory.create()
				.toJson();
    }

	@RequiresPermissions("cmsLabel:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/toEdit/{id}")
    public String toEdit(@PathVariable Long id, Model model) throws Exception {
		CmsLabel cmsLabel = cmsLabelService.getById(id);

		model.addAttribute("tenants", getAllTenants());
    	model.addAttribute("cmsLabel", cmsLabel);
		model.addAttribute("active", "cmsLabel");
		model.addAttribute("statusValues", LabelStatus.values());
		model.addAttribute("categoryList", cmsCategoryService.getAll(new CmsCategoryWhere()));
        return "cmsLabel/cmsLabelEdit";
    }

	@RequiresPermissions("cmsLabel:toAdd")
	@RequestMapping(method = RequestMethod.GET, value = "/toAdd")
    public String toAdd(Model model) throws Exception {
		model.addAttribute("tenants", getAllTenants());
		model.addAttribute("active", "cmsLabel");
		model.addAttribute("statusValues", LabelStatus.values());
		model.addAttribute("categoryList", cmsCategoryService.getAll(new CmsCategoryWhere()));
        return "cmsLabel/cmsLabelEdit";
    }

	@RequiresPermissions("cmsLabel:view")
	@RequestMapping(method = RequestMethod.GET, value = "/view/{id}")
    public String view(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	CmsLabel cmsLabel = cmsLabelService.getById(id);
		cmsLabel.setTenant(tenantService.getById(cmsLabel.getTenantId()));
		model.addAttribute("tenants", getAllTenants());
    	model.addAttribute("cmsLabel", cmsLabel);
		model.addAttribute("active", "cmsLabel");
        return "cmsLabel/cmsLabelView";
    }

	@RequiresPermissions("cmsLabel:edit")
	@ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/view/json/{id}")
    public String viewJson(@PathVariable Long id, Model model) throws Exception {
		Objects.requireNonNull(id);
    	CmsLabel cmsLabel = cmsLabelService.getById(id);
    	return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", cmsLabel)
				.toJson();
    }

	@RequiresPermissions("cmsLabel:list")
	@RequestMapping(method = RequestMethod.GET, value = "/list")
	public String list(@ModelAttribute CmsLabelWhere cmsLabelWhere, Model model) {
		cmsLabelWhere.setOrder("id desc");
		PageResult<CmsLabel> cmsLabels = cmsLabelService.findByCmsLabelWhere(cmsLabelWhere);
		cmsLabels.getBeans().forEach(l-> {
			l.setTenant(tenantService.getById(l.getTenantId()));
			l.setCategory(cmsCategoryService.getById(l.getCategoryId()));
		});

		model.addAttribute("tenants", getAllTenants());
		model.addAttribute("cmsLabels", cmsLabels);
		model.addAttribute("active", "cmsLabel");
		model.addAttribute("cmsLabel", cmsLabelWhere);
		model.addAttribute("statusValues", LabelStatus.values());
		model.addAttribute("categoryList", cmsCategoryService.getAll(new CmsCategoryWhere()));
		return "cmsLabel/cmsLabelList";
	}

	@RequiresPermissions("cmsLabel:edit")
	@RequestMapping(method = RequestMethod.GET, value = "/list/json")
	@ResponseBody
	public String listJson(@ModelAttribute CmsLabelWhere cmsLabelWhere, Model model) {
		cmsLabelWhere.setOrder("id desc");
		PageResult<CmsLabel> cmsLabels = cmsLabelService.findByCmsLabelWhere(cmsLabelWhere);
		return JsonViewFactory.create()
				.setDateFormat("yyyy-MM-dd hh:mm:ss")
				.put("data", cmsLabels)
				.toJson();
	}
	
	@RequiresPermissions("cmsLabel:delete")
	@RequestMapping(method = RequestMethod.GET, value = "/deleteById/json/{id}")
	@ResponseBody
	public String deleteJsonById(@PathVariable("id")Long id) {
		CmsLabel cmsLabel = cmsLabelService.getById(id);
        Objects.requireNonNull(cmsLabel);
		cmsLabelService.deleteById(id);
        return JsonViewFactory.create()
				.toJson();
        }

	
}